package cate.game.server.service;

import cate.game.GameBody;
import cate.game.ManyGame;
import cate.game.mail.MailDataSource;
import cate.game.util.GameSumInfo;
import cp.solution.business.gm.GmMailEntity;
import cp.solution.manage.PrimaryClient;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.concurrent.CopyOnWriteArrayList;

@Slf4j
@Service
public class MailService implements MailDataSource {

    ManyGame games;

    public void setup(ManyGame games) {
        this.games = games;
    }

    PrimaryClient pc() {
        return games.api.pc();
    }

    // Map<exeId, GmMailEntity>
    private Map<Integer, GmMailEntity> mailMap = new HashMap<>();
    private List<GmMailEntity> mailList = new CopyOnWriteArrayList<>();

    @Override
    public List<GmMailEntity> getGmMailList() {
        return mailList;
    }

    private int maxExeId = 0;

    public synchronized void update() {
        if(games == null) {
            log.warn("邮件数据即将更新,但是games没有准备好");
            return;
        } else {
            log.debug("邮件数据即将更新");
        }
        try {
            List<GmMailEntity> some = pc().console.gmMail.retrieve(maxExeId + 1);
            //log.info("some={}", some);
            GameSumInfo gsi = games.sum();

            boolean needUpdate = false;
            for (GmMailEntity m : some) {
                if (m.getExeId() > maxExeId) {
                    maxExeId = m.getExeId();
                }
                if(obviouslyNot(m, gsi)) {
                    continue;
                }
                boolean b = mailMap.containsKey(m.getExeId());
                if (!b) {
                    needUpdate = true;
                    mailMap.put(m.getExeId(), m);
                    mailList.add(m);
                }
            }
            if (needUpdate) {
                // 通知符合条件的在线玩家
                Iterator<GameBody> iter = games.getAll().iterator();
                while (iter.hasNext()) {
                    GameBody game = iter.next();
                    game.mail.gmMail.onUpdate(mailList);
                }
            }
        } catch (Exception e) {
            log.error("", e);
        }
    }

    /** 是否 显然不属于这些GameBody们 */
    private boolean obviouslyNot(GmMailEntity m, GameSumInfo gsi) {
        if(StringUtils.isNotBlank(m.getGroupCode()) && !gsi.containsGroupCode(m.getGroupCode())) {
            return true;
        }
        if(StringUtils.isNotBlank(m.getSrvIds())) {
            boolean notMatch = true;
            for(Integer srvId : gsi.srvIdList) {
                if(m.matchSrvId(srvId)) {
                    notMatch = false;
                    break;
                }
            }
            if(notMatch) return true;
        }

        boolean matched = false;
        for(Integer srvId : gsi.srvIdList) {
            if(m.getSrvIdBegin() != null && srvId < m.getSrvIdBegin()) {
                continue;
            }
            if(m.getSrvIdEnd() != null && srvId > m.getSrvIdEnd()) {
                continue;
            }
            matched = true;
            break;
        }
        if(!matched) {
            return true;
        }
        return false;
    }
}
